#include "Peca.h"

Peca::Peca(int c)
{
	cor = c;
	if(cor)
	{
		float PretoAmbient[4] = {0,0,0,0};
		float PretoDifuse[4] = {0.25,0.25,0.25,0.0};
		float PretoSpec[4] = {0.1,0.1,0.1,0.0};
		float PretoShineness = 10.0;
		corpeca = new CGFappearance(PretoAmbient,PretoDifuse,PretoSpec,PretoShineness);
	}
	else
	{
		float VAmbient[4] = {0.0,0.0,0.0,0};
		float VDifuse[4] = {0.25,0.0,0.0,1.0};
		float VSpec[4] = {0.1,0.0,0.0,0.0};
		float VShineness = 10.0;
		corpeca = new CGFappearance(VAmbient,VDifuse,VSpec,VShineness);
	}
}

int Peca::getCor()
{
	return cor;
}

void Peca::draw()
{
	int slices = 50;
	int stacks = 1;
	float top = 0.5;
	float base = 0.5;
	float height = 0.2;
	float pi = acos(-1.0);
	float AngleVertice = 2.0 * pi / (float) slices;
	float angle,nextangle;
	float HCuts = height / stacks;
	float actualBase;
	float nextBase;
	corpeca->apply();
	for(int h = 0; h < stacks; h++)
	{
		glPushMatrix();
		glTranslatef(0,HCuts*h,0);
		if(top > base)
		{
			actualBase = (((top - base)/stacks) * h) + base;
			nextBase = (((top - base)/stacks) * (h+1)) + base;
		}
		else if(top < base)
		{
			actualBase = (((top - base)/stacks) * h) + base;
			nextBase = (((top - base)/stacks) * (h+1)) + base;
		}
		else actualBase = nextBase = base;
		for(int i = 1; i <= slices; i++ )
		{
			angle =  i * AngleVertice; 	
			nextangle =  (i+1) * AngleVertice; 	
			glBegin(GL_POLYGON);	
				glNormal3f(cos(angle),0,sin(angle));
				glTexCoord3f(0,0,0);
				glVertex3f(actualBase*cos(angle),0,actualBase*sin(angle));
				glNormal3f(cos(angle),0,sin(angle));
				glTexCoord3f(0,1,0);
				glVertex3f(nextBase*cos(angle),HCuts,nextBase*sin(angle));
				glNormal3f(cos(nextangle),0,sin(nextangle));
				glTexCoord3f(1,1,0);
				glVertex3f(nextBase*cos(nextangle),HCuts,nextBase*sin(nextangle));
				glNormal3f(cos(nextangle),0,sin(nextangle));
				glTexCoord3f(1,0,0);
				glVertex3f(actualBase*cos(nextangle),0,actualBase*sin(nextangle));
			glEnd();
		}
		glPopMatrix();
	}
	glPushMatrix();
		glTranslatef(0,height,0);
		glRotatef(180,1,0,0);
		glBegin(GL_POLYGON);
		for(int i = 1; i <= slices; i++)
		{
			glNormal3f(cos(AngleVertice*i),-1,sin(AngleVertice*i));
			glVertex3d(cos(AngleVertice*i)*top,0,sin(AngleVertice*i)*top);
		}
		glEnd();
	glPopMatrix();
	glPushMatrix();
		glBegin(GL_POLYGON);
		for(int i = 1; i <= slices; i++)
		{
			glNormal3f(cos(AngleVertice*i),-1,sin(AngleVertice*i));
			glVertex3d(cos(AngleVertice*i)*base,0,sin(AngleVertice*i)*base);
		}
		glEnd();
	glPopMatrix();
}